package com.zhenyi.api;

import java.io.File;
import java.util.ArrayList;

import com.cenjiawen.common.IndexController;
import com.cjw.model.DBMap;
import com.jfinal.kit.StrKit;
import com.utils.PathUtils;
import com.zhenyi.core.VueUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Singleton;

public class FenciController extends IndexController {

	@Override
	public void index() {
		String text = getPara("text", "");
		if (StrKit.isBlank(text)) {
			renderFailure();
			return;
		}
		FenciUtils utils = Singleton.get(FenciUtils.class);
		String result = utils.run(text);
		renderSuccess(result);
	}

	public void template() {
		ArrayList<DBMap> products = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			DBMap product = new DBMap();
			product.put("title", "产品" + i);
			product.put("id", i);
			products.add(product);
		}
		DBMap info = new DBMap();
		info.put("title", "helllo");
		info.put("productId", 2);
		info.put("isHot", 1);
		info.put("startDate", "2019-01-12");
		info.put("endDate", "2019-02-12");
		String json = VueUtils.getVueData(info);
		setAttr("info", json);
		setAttr("products", VueUtils.getVueData(products));
		render("/data/template/gen1.html");
	}

	public static void main(String[] args) {

		// TemplateConfig cfg = new TemplateConfig();
		// System.out.println(cfg.getPath());
		// System.out.println(cfg.getResourceMode().name());
		// Engine engine = TemplateUtil.createEngine(new TemplateConfig());
		// Template template = engine.getTemplate("Hello ${name}");
		// // Dict本质上为Map，此处可用Map
		// String result = template.render(Dict.create().set("name", "Hutool"));

	}

	public void upload() {
		File file = getFileParam("file");
		String name = getPara("name");
		String today = DateUtil.today();
		String bucket = getPara("bucket", "zhenyi");
		File dest = new File(PathUtils.getFile("storage", bucket, today, name));
		FileUtil.move(file, dest, true);
		renderSuccess();
	}

}
